package com.vogue.supplymall.match.domain.dao.impl;

import java.awt.peer.LightweightPeer;
import java.util.List;

import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.vogue.supplymall.account.domain.model.User;
import com.vogue.supplymall.match.domain.dao.MatchOrganDao;
import com.vogue.supplymall.match.domain.mapper.MatchOrganMapper;
import com.vogue.supplymall.match.domain.model.MatchOrgan;
import com.vogue.supplymall.shop.domain.model.Shop;;

@Repository("matchOrganDao")
public class MatchOrganDaoImpl extends BaseDaoImpl<MatchOrgan, Long> implements MatchOrganDao{
	@Autowired
	private MatchOrganMapper matchOrganMapper;
	
	@Override
	public Long checkOrganIsExist(String matchId, Long sysCodeId) {
		Long id=(long) 0;
		List<Long> list=matchOrganMapper.checkOrganIsExist(sysCodeId, matchId);
		if(list.size()>0) {
			id=list.get(0);
		}
		return id;
	}

	@Override
	public List<String> getAllOrganIdBySysCode(Long sysCodeId) {
		//String sql="select matchid from match_organ where matchId is not null and sysCodeId="+sysCodeId;
		List<String> list=matchOrganMapper.getAllOrganIdBySysCode(sysCodeId);
		return list;
	}

	@Override
	public int deleteOrangInfo(String ids, Long sysCode) {
		String sql="delete from match_organ "
				+ " where sysCode='"+sysCode+"' and matchId in ("+ids+")";
		int resultInt=this.delete(sql);
		return resultInt;
	}

	@Override
	public int deleteShopInfo(String ids, Long sysCodeId) {
		String sql="update shop,match_organ mo set shop.disabledflag=1"
				+ " where shop.sysCodeId='"+sysCodeId+"' and "
				+ " shop.organId=mo.id and mo.matchId in ("+ids+")"
				+ " and mo.sysCodeId='"+sysCodeId+"'";
		
		int resultInt=this.update(sql);
		
		return resultInt;
	}

	@Override
	public int updatePId(Long sysCodeId) {
		String sql="update match_organ mo1,match_organ mo2 set mo1.pid=mo2.id"
				+ " where mo1.syscodeid='"+sysCodeId+"' and mo2.sysCodeId='"+sysCodeId+"'"
				+ " and mo1.type in (2,3) and mo2.type=1";
		int updateCnt=this.update(sql);
		return updateCnt;
	}

	@Override
	public MatchOrgan getMatchOrganByMatchId(Long sysCodeId,String matchId) {
		 
		List<MatchOrgan> list=matchOrganMapper.getMatchOrganByMatchId(sysCodeId,matchId);
		 if (list.size()>0) {
			return  list.get(0);
					
		}else {
			return null;
		}
		
	}

	@Override
	public MatchOrgan getEnterpriseByMatchId(Long sysCodeId) {
		List<MatchOrgan> list=matchOrganMapper.getEnterpriseByMatchId(sysCodeId);
		 if (list.size()>0) {
			return  list.get(0);
					
		}else {
			return null;
		}
	}

	@Override
	public int deleteOrganShopImgByMatchId(String matchId,Long syscodeId) {
		String sql="delete from organ_shop_img "
				+ " where  organid  in ("
				+ " select id from match_organ  where  matchid  ='"+matchId+"' and syscodeid="+syscodeId+") "  ;
		int resultInt=this.delete(sql);
		return resultInt;
	}
	@Override
	public int deleteAddressByMatchId(String matchId,Long  syscodeId) {
		String sql="delete from address "
				+ " where  organid  in ("
				+ " select id from match_organ  where  matchid  ='"+matchId+"' and syscodeid="+syscodeId+") "  ;
		int resultInt=this.delete(sql);
		return resultInt;
	}

	@Override
	public int deleteRelUserOrganByMatchId(String matchId,Long syscodeId) {
		String sql="delete from rel_user_organ "
				+ " where  userid  in ("
				+ " select id from base_user  where  matchid  ='"+matchId+"' and syscodeid="+syscodeId+") "  ;
		int resultInt=this.delete(sql);
		return resultInt;
	}

//	@Override
//	public int deleteUserByMatchId(String matchId) {
//		String sql="delete from base_user "
//				+ " where  matchid  ='"+matchId+"'";
//		int resultInt=this.delete(sql);
//		return resultInt;
//	}

	@Override
	public int deleteRelShopCategoryBySysCodeId(Long sysCodeId) {
		String sql="delete from rel_shop_category "
				+ " where  syscodeid  ="+sysCodeId;
		int resultInt=this.delete(sql);
		return resultInt;
	}

    /**
     * Count valid shop long.
     * admin-business使用，查询所有开通的店铺
     *
     * @return the long
     */
    public Long countValidOrgan(){
        return matchOrganMapper.countValidOrgan();
    }

	@Override
	public int deleteStoreBySyscodeId(Long syscodeId) {
		String sql="delete mo from match_organ mo" + 
				" left join rel_user_organ ruo on mo.syscodeid=ruo.syscodeid and mo.id=ruo.organid" + 
				" where mo.type=4 and ruo.id is null and mo.matchid!='woshicangku' and mo.syscodeid="+syscodeId;
		
		return this.delete(sql);
	}

}
